﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using ZLDC.Common.Estate;
using ZLDC.Topo.Model.Estate;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ZLDC.Topo.BLL.Estate
{
    public class AreaBLL
    {
                                        public static string AreaList(int parentid)
        {
            DataTable dt = null;
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.AreaDAL.p_area_listall_table(parentid, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

                                        public static List<AreaInfo> AreaList2(int parentid)
        {
            List<AreaInfo> list = new List<AreaInfo>();

            using (_Database db = new _Database("estate"))
            {
                list = ZLDC.DAL.Estate.AreaDAL.p_area_listall(parentid, db);
            }
         
            return list;
        }
        public static List<AreaInfo> AreaList3(int parentid,string IdList)
        {
            List<AreaInfo> list = new List<AreaInfo>();

            using (_Database db = new _Database("estate"))
            {
                list = ZLDC.DAL.Estate.AreaDAL.p_area_CityListAll(parentid,IdList, db);
            }

            return list;
        }

                                        public static string AreaListByHtml(int parentid)
        {
            string resultStr = string.Empty;
            List<AreaInfo> list = null;

            if (parentid >= 0)
            {
                using (_Database db = new _Database("estate"))
                {
                    list = ZLDC.DAL.Estate.AreaDAL.p_area_listall(parentid, db);
                }

                if (list != null && list.Count > 0)
                {
                    resultStr += "<ul>";
                    foreach (AreaInfo item in list)
                    {
                        resultStr += "<li id=\"" + item.Id + "\"><span id=\"1_" + item.Id + "\">" + item.Name + "</span>";                        resultStr += "<ul class='ajax'><li>{url:../Handler/HandlerArea.ashx?pid=" + item.Id + "}</li></ul>";
                        resultStr += "</li>";
                    }
                    resultStr += "</ul>";
                }
            }

            return resultStr;
        }

                                                public static string AreaSave(int id, string name, string namePy, int parentId, int delFlag, int ord)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.AreaDAL.p_area_save(id, name, namePy, parentId, delFlag, ord, out outmsg, out newid, db);
            }

            return outmsg;
        }

                                                public static string AreaDel(int id)
        {
            string outmsg = "";
        
            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.AreaDAL.p_area_delete(id, out outmsg, db);
            }

            return outmsg;
        }

                                public static void LoadCity(HtmlSelect ddlCity, int parentid)
        {
            using (_Database db = new _Database("estate"))
            {
                DataTable dt = ZLDC.DAL.Estate.AreaDAL.p_area_listall_table(parentid, db);
                ddlCity.DataSource = dt;
                ddlCity.DataTextField = "name";
                ddlCity.DataValueField = "id";
                ddlCity.DataBind();

                if (dt == null || dt.Rows.Count == 0)
                {
                    ListItem item = new ListItem();
                    item.Text = "-- 无 --";
                    item.Value = "0";
                    ddlCity.Items.Insert(0, item);
                }
            }
        }
                                        
        public static string SelectAreaNameById(int id) 
        {
            return ZLDC.DAL.Estate.AreaDAL.SelectAreaNameById(id);
        }
    }
}
